package top.glkj.hms.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.glkj.hms.dao.ResourceDao;
import top.glkj.hms.entity.Resource;
import top.glkj.hms.service.ResourceService;
import top.glkj.hms.utils.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author : xgl
 * @version : 0.0.1
 * @date :2020/11/16 23:42
 */
@Service
@Slf4j
public class ResourceServiceImpl implements ResourceService {

    private final ResourceDao resourceDao;

    @Autowired
    public ResourceServiceImpl(ResourceDao resourceDao) {
        this.resourceDao = resourceDao;
    }

    /**
     * 获取所有用户
     *
     * @return List<T>
     */
    @Override
    public Result getAll() {
        List<Resource> all = resourceDao.findAll();
       return getNotEmptyAll(all);
    }

    /**
     * 获取所有用户，按分页方式
     *
     * @param id id
     * @return T t
     */
    @Override
    public Result getOne(Long id) {
        Resource resource = resourceDao.findById(id).orElse(null);
        if (resource == null) {
            return ResultFactory.isError(StaticValue.DATA_NOT_FOUND, null);
        } else {
            return ResultFactory.isSuccess(StaticValue.GET_DATA_SUCCESS, resource);
        }
    }

    /**
     * 修改或者保存
     *
     * @param resource T t
     * @return true or false
     */
    @Override
    public Result save(Resource resource) {
        try {
            Resource save = resourceDao.save(resource);
            return ResultFactory.isSuccess(StaticValue.SUCCESS_MESSAGE, save);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultFactory.isException(StaticValue.ERROR_MESSAGE, null);
        }
    }

    /**
     * 彻底从数据库中删除实体{T}
     *
     * @param resource T t
     * @return true or false
     */
    @Override
    public Result delete(Resource resource) {
        try {
            resourceDao.delete(resource);
            return ResultFactory.isSuccess(StaticValue.DELETE_DATA_ERROR);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultFactory.isError(StaticValue.ERROR_MESSAGE);
        }
    }

    /**
     * 标记删除
     *
     * @param resource T t
     * @return true or false
     */
    @Override
    public Result remove(Resource resource) {
        resource.setDel(true);
        resource.setStatus(Status.Delete);
        Result save = save(resource);
        if (ResultFactory.isSuccess(save)) {
            return ResultFactory.isSuccess(StaticValue.DELETE_DATA_SUCCESS, save);
        } else {
            return ResultFactory.isException(StaticValue.DELETE_DATA_ERROR);
        }
    }

    /**
     * 查询数据库中所有路径
     *
     * @return 数据库中所有路径
     */
    @Override
    public List<Resource> findAllResource() {
        return resourceDao.findAll();
    }

    /**
     * 分页查询
     *
     * @param page 当前页码
     * @param size 页面大小
     * @return page Role
     */
    @Override
    public Result getResourcesByPageAndSize(int page, int size) {
        GlUtils<Resource, ResourceDao> glUtils = new GlUtils<>();
        return glUtils.getAllByPageAndSize(page, size, resourceDao);
    }

    /**
     * 分页条件查询
     *
     * @param page    当前页码
     * @param size    页面大小
     * @param argName 参数名
     * @param value   参数值
     * @return page User
     */
    @Override
    public Result getAllResourcesByPageAndSizeAndArgs(int page, int size, String argName, String value) {
        GlUtils<Resource, ResourceDao> glUtils = new GlUtils<>();
        return glUtils.getAllByPageAndSizeAndArgs(page, size, argName, value, resourceDao);
    }

    /**
     * 分页查询
     *
     * @param page 当前页码
     * @param size 页面大小
     * @param args 参数键值对
     * @return page User
     */
    @Override
    public Result getAllResourcesByPageAndSizeAndAnyArgs(int page, int size, Map<String, String> args) {
        GlUtils<Resource, ResourceDao> glUtils = new GlUtils<>();
        return glUtils.getAllByPageAndSizeAndAnyArgs(page, size, args, resourceDao);
    }

    /**
     * 更新资源
     *
     * @param resource 资源
     * @return 更新结果
     */
    @Override
    public Result updateResource(Resource resource) {
        resource.setUpdateUserId(getCurrentUser().getId());
        resource.setUpdateTime(new Date());
        return save(resource);
    }

    /**
     * 添加资源
     *
     * @param resource
     * @return 更新结果
     */
    @Override
    public Result addResource(Resource resource) {
        resource.setUpdateUserId(getCurrentUser().getId());
        resource.setUpdateTime(new Date());
        resource.setCreateUserId(getCurrentUser().getId());
        resource.setCreateTime(new Date());
        return save(resource);
    }

    /**
     * 删除资源
     *
     * @param rid 资源id
     * @return 更新结果
     */
    @Override
    public Result deleteResource(long rid) {
        try {
            resourceDao.deleteById(rid);
            return ResultFactory.isSuccess(StaticValue.DELETE_DATA_SUCCESS);
        }catch (Exception e){
            log.error(e.getMessage());
            return ResultFactory.isError(StaticValue.DELETE_DATA_ERROR,e);
        }
    }

    @Override
    public String getUrl() {
        return null;
    }
}
